Skip to content

Conversation

@dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Oct 10, 2025

Closes #3185

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

If you have comments or can explain your changes, please do so below

  • Implementent on nw.dtypes
  • Check if stable.* needs empty __slots__ (It did)

@dangotbanned
Copy link
Member Author

I'm thinking Schema may be a good candidate as well (provided it is allowed on dict subclasses)

@dangotbanned dangotbanned marked this pull request as ready for review October 10, 2025 19:09
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dangotbanned - I am quite a big fan of slots so I am definitely onboarded πŸ˜‚

I left a comment to (hopefully) lower some of the maintenance effort

@dangotbanned dangotbanned marked this pull request as draft October 11, 2025 19:08
@dangotbanned dangotbanned mentioned this pull request Oct 11, 2025
73 tasks
@dangotbanned dangotbanned marked this pull request as ready for review October 11, 2025 22:13
dangotbanned added a commit that referenced this pull request Oct 12, 2025
Bug discovered by @FBruzzesi
#3194 (comment)

Now I need to fix 200/222 failures πŸ˜‚
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dangotbanned - I left a non-bloking nitpick comment for the error message but feel free to ignore it if you don't like it much better.

Co-authored-by: Francesco Bruzzesi <[email protected]>
@dangotbanned dangotbanned merged commit feb02af into main Oct 12, 2025
30 of 31 checks passed
@dangotbanned dangotbanned deleted the dtype-__slots__ branch October 12, 2025 22:06
@jonded94
Copy link

Sorry for asking here (I'm also not a maintainer of this library, but just sumbled over this issue):

Instead of manually having to explicitly set __slots__ everywhere (which introduces maintainance load and introduces the risk of setting them improperly), why not use typing.NamedTuple (if immutability is not a problem) or dataclass with slots=True (if mutability is required)?
There are already tools in the stdlib that do these kinds of things automatically for you.

Sorry again, just a curious question from an outsider!

@FBruzzesi
Copy link
Member

Sorry again, just a curious question from an outsider!

Hey @jonded94 - no need to apologize, actually thank you for questioning these kind of choices. We should definitely not dive into a solution without thinking about pros and cons of different approaches.

which introduces maintainance load and introduces the risk of setting them improperly

I feel you, I tried to propose a metaclass that can lower such burden :) and #3201 is using something towards such direction, so let's see if it makes it in the main branch soon as well.

why not use typing.NamedTuple (if immutability is not a problem) or dataclass with slots=True (if mutability is required)? There are already tools in the stdlib that do these kinds of things automatically for you.

We probably give it for granted in many places, but the main idea is that we want to have an API which is very close to the polars one. That's not always possible, but I would rather not have a discrepancy such as:

from dataclasses import is_dataclass
from polars import DataType
from narwhals.dtypes import DType

is_dataclass(DataType)
# False

is_dataclass(DType)
# True

or similarly:

from collections import namedtuple
import polars as pl

Int64 = namedtuple('Int64', field_names=())

isinstance(pl.Int64(), tuple), isinstance(Int64(), tuple)
# False, True

I hope it makes sense πŸ˜‡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add __slots__ to all DTypes

4 participants